{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.7.3 64-bit ('ml2': pyenv)"
  },
  "interpreter": {
   "hash": "8caa4319d59a734f94e739df6b594acde678ec5f2e3eb7152d087fc4a1992669"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# AIGER Circuits"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Parsing"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "from ml2.aiger import parse_no_header"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "#Note the special order of inputs, ands, latches, and outputs\n",
    "circuit_str = \"2\\n4\\n6\\n8\\n10\\n17 11 15\\n10 11\\n0\\n0\\n1\\n0\\n16\""
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "circuit = parse_no_header(circuit_str, 5, 5, components=['header', 'inputs', 'ands', 'latches', 'outputs'])"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "str(circuit)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'aag 8 5 1 5 1\\n2\\n4\\n6\\n8\\n10\\n10 11\\n0\\n0\\n1\\n0\\n16\\n17 11 15'"
      ]
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "circuit_str = \"2\\n4\\n6\\n8\\n10\\n12 7\\n0\\n0\\n1\\n0\\n16\\n14 9 6\\n16 14 12\\ni0 i0\\ni1 i1\\ni2 i2\\ni3 i3\\ni4 i4\\nl0 l0\\no0 o0\\no1 o1\\no2 o2\\no3 o3\\no4 o4\""
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "circuit = parse_no_header(circuit_str, 5, 5)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "str(circuit)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'aag 8 5 1 5 2\\n2\\n4\\n6\\n8\\n10\\n12 7\\n0\\n0\\n1\\n0\\n16\\n14 9 6\\n16 14 12\\ni0 i0\\ni1 i1\\ni2 i2\\ni3 i3\\ni4 i4\\nl0 l0\\no0 o0\\no1 o1\\no2 o2\\no3 o3\\no4 o4'"
      ]
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## AIGER Encoder"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "from ml2.aiger import AIGERSequenceEncoder\n",
    "from ml2.data.vocabulary import Vocabulary"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "source": [
    "token_to_id = {\n",
    "    '<p>': 0,\n",
    "    '0': 1,\n",
    "    '1': 2,\n",
    "    '2': 3,\n",
    "    '3': 4,\n",
    "    '4': 5,\n",
    "    '5': 6,\n",
    "    '6': 7,\n",
    "    '7': 8,\n",
    "    '8': 9,\n",
    "    '9': 10,\n",
    "    '10': 11,\n",
    "    '11': 12,\n",
    "    '12': 13,\n",
    "    '13': 14,\n",
    "    '14': 15,\n",
    "    '15': 16,\n",
    "    '16': 17,\n",
    "    '17': 18,\n",
    "    '18': 19,\n",
    "    '19': 20,\n",
    "    '20': 21,\n",
    "    '21': 22,\n",
    "    '22': 23,\n",
    "    '23': 24,\n",
    "    '24': 25,\n",
    "    '25': 26,\n",
    "    '26': 27,\n",
    "    '27': 28,\n",
    "    '28': 29,\n",
    "    '29': 30,\n",
    "    '30': 31,\n",
    "    '31': 32,\n",
    "    '32': 33,\n",
    "    '33': 34,\n",
    "    '34': 35,\n",
    "    '35': 36,\n",
    "    '36': 37,\n",
    "    '37': 38,\n",
    "    '38': 39,\n",
    "    '39': 40,\n",
    "    '40': 41,\n",
    "    '<s>': 42,\n",
    "    '<e>': 43,\n",
    "    '<c>': 44,\n",
    "    '<l>': 45,\n",
    "    '<n>': 46,\n",
    "    '<r>': 47,\n",
    "    '<u>': 48\n",
    "}"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "source": [
    "vocab = Vocabulary(token_to_id)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "INFO:root:Constructed vocabulary containing 49 tokens\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "source": [
    "encoder = AIGERSequenceEncoder(True, True, 128, components=['header', 'inputs', 'latches', 'outputs', 'ands'], encode_start=True, encode_realizable=True, inputs=['i0', 'i1', 'i2', 'i3', 'i4'], outputs=['o0', 'o1', 'o2', 'o3', 'o4'], unfold_negations=True, unfold_latches=True, vocabulary=vocab)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "source": [
    "circuit_str = \"aag 19 5 3 5 11\\n2\\n4\\n6\\n8\\n10\\n12 30\\n14 35\\n16 39\\n26\\n0\\n0\\n0\\n0\\n18 17 9\\n20 13 5\\n22 20 15\\n24 23 13\\n26 24 19\\n28 19 14\\n30 28 20\\n32 16 14\\n34 33 25\\n36 19 13\\n38 37 33\\ni0 i0\\ni1 i1\\ni2 i2\\ni3 i3\\ni4 i4\\nl0 l0\\nl1 l1\\nl2 l2\\no0 o0\\no1 o1\\no2 o2\\no3 o3\\no4 o4\""
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "source": [
    "encoder.encode(circuit_str)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "source": [
    "encoder.error"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "source": [
    "ids = encoder.ids\n",
    "ids"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[42,\n",
       " 47,\n",
       " 20,\n",
       " 6,\n",
       " 1,\n",
       " 6,\n",
       " 12,\n",
       " 46,\n",
       " 2,\n",
       " 46,\n",
       " 3,\n",
       " 46,\n",
       " 4,\n",
       " 46,\n",
       " 5,\n",
       " 46,\n",
       " 6,\n",
       " 46,\n",
       " 14,\n",
       " 46,\n",
       " 1,\n",
       " 46,\n",
       " 1,\n",
       " 46,\n",
       " 1,\n",
       " 46,\n",
       " 1,\n",
       " 46,\n",
       " 10,\n",
       " 44,\n",
       " 45,\n",
       " 44,\n",
       " 20,\n",
       " 44,\n",
       " 5,\n",
       " 46,\n",
       " 11,\n",
       " 44,\n",
       " 45,\n",
       " 16,\n",
       " 44,\n",
       " 3,\n",
       " 46,\n",
       " 12,\n",
       " 11,\n",
       " 44,\n",
       " 45,\n",
       " 44,\n",
       " 18,\n",
       " 46,\n",
       " 13,\n",
       " 44,\n",
       " 12,\n",
       " 44,\n",
       " 45,\n",
       " 16,\n",
       " 46,\n",
       " 14,\n",
       " 13,\n",
       " 44,\n",
       " 10,\n",
       " 46,\n",
       " 15,\n",
       " 44,\n",
       " 10,\n",
       " 45,\n",
       " 44,\n",
       " 18,\n",
       " 46,\n",
       " 16,\n",
       " 15,\n",
       " 11,\n",
       " 46,\n",
       " 17,\n",
       " 45,\n",
       " 44,\n",
       " 20,\n",
       " 45,\n",
       " 44,\n",
       " 18,\n",
       " 46,\n",
       " 18,\n",
       " 44,\n",
       " 17,\n",
       " 44,\n",
       " 13,\n",
       " 46,\n",
       " 19,\n",
       " 44,\n",
       " 10,\n",
       " 44,\n",
       " 45,\n",
       " 16,\n",
       " 46,\n",
       " 20,\n",
       " 44,\n",
       " 19,\n",
       " 44,\n",
       " 17,\n",
       " 43,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0]"
      ]
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "source": [
    "encoder.decode(ids)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "source": [
    "encoder.error"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "source": [
    "encoder.sequence"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'aag 19 5 3 5 11\\n2\\n4\\n6\\n8\\n10\\n12 39\\n14 30\\n16 35\\n26\\n0\\n0\\n0\\n0\\n18 13 9\\n20 15 5\\n22 20 17\\n24 23 15\\n26 24 19\\n28 19 16\\n30 28 20\\n32 12 16\\n34 33 25\\n36 19 15\\n38 37 33\\ni0 i0\\ni1 i1\\ni2 i2\\ni3 i3\\ni4 i4\\nl0 l0\\nl1 l1\\nl2 l2\\no0 o0\\no1 o1\\no2 o2\\no3 o3\\no4 o4'"
      ]
     },
     "metadata": {},
     "execution_count": 19
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "source": [
    "encoder.realizable"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "metadata": {}
  }
 ]
}